package com.hooper.executor.demo;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/03/02/21:25
 */
public class TestFixedThreadPool {
    @Test
    public void test1() {
        //空参创建
        ExecutorService es = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            es.submit(new MyTask());
        }
    }

    @Test
    public void test2() {
        ThreadFactoryBuilder factoryBuilder = ThreadFactoryBuilder.create().setPriority(Thread.MAX_PRIORITY)
                .setDaemon(false)
                .setNamePrefix("my-thread:")
                .setThreadFactory(Thread::new);
        //基于ThreadFactory创建；可对线程进行设置 如：名称，优先级等
        ExecutorService es = Executors.newFixedThreadPool(4, factoryBuilder.build());
        for (int i = 0; i < 10; i++) {
            es.submit(() -> System.out.println(Thread.currentThread().getName() + " run >>>"));
        }
    }
}